

export default function md5(data) {
	// for test/debug
	function fflog(msg) {
		try {
			// console.log(msg);
		} catch (e) {
			console.error(e);
		}
	}

	// convert number to (unsigned) 32 bit hex, zero filled string
	function toZerofilledHex(n) {
		const t1 = (n >>> 24).toString(16);
		const t2 = (n & 0x00FFFFFF).toString(16);

		return "00".substr(0, 2 - t1.length) + t1 +
			"000000".substr(0, 6 - t2.length) + t2;
	}

	// convert array of chars to array of bytes (note: Unicode not supported)
	function charsToBytes(ac) {
		let retval = [];

		for (let i = 0; i < ac.length; i++) {
			retval = retval.concat(strToBytes(ac[i]));
		}

		return retval;
	}


	// convert a 64 bit unsigned number to array of bytes. Little endian
	function int64ToBytes(num) {
		const retval = [];

		for (let i = 0; i < 8; i++) {
			retval.push(num & 0xFF);
			num = num >>> 8;
		}

		return retval;
	}

	//  32 bit left-rotation
	function rol(num, places) {
		return ((num << places) & 0xFFFFFFFF) | (num >>> (32 - places));
	}

	// The 4 MD5 functions
	function fF(b, c, d) {
		return (b & c) | (~b & d);
	}

	function fG(b, c, d) {
		return (d & b) | (~d & c);
	}

	function fH(b, c, d) {
		return b ^ c ^ d;
	}

	function fI(b, c, d) {
		return c ^ (b | ~d);
	}

	// pick 4 bytes at specified offset. Little-endian is assumed
	function bytesToInt32(arr, off) {
		return (arr[off + 3] << 24) | (arr[off + 2] << 16) | (arr[off + 1] << 8) | (arr[off]);
	}

	/*
	 Conver string to array of bytes in UTF-8 encoding
	 See:
	 http://www.dangrossman.info/2007/05/25/handling-utf-8-in-javascript-php-and-non-utf8-databases/
	 http://stackoverflow.com/questions/1240408/reading-bytes-from-a-javascript-string
	 How about a String.getBytes(<ENCODING>) for Javascript!? Isn't it time to add it?
	 */
	function strToBytes(str) {
		// alert("got " + str.length + " chars")
		const retval = [];

		for (let i = 0; i < str.length; i++)
			if (str.charCodeAt(i) <= 0x7F) {
				retval.push(str.charCodeAt(i));
			} else {
				const tmp = encodeURIComponent(str.charAt(i)).substr(1).split('%');

				for (let j = 0; j < tmp.length; j++) {
					retval.push(parseInt(tmp[j], 0x10));
				}
			}

		return retval;
	}






	// convert the 4 32-bit buffers to a 128 bit hex string. (Little-endian is assumed)
	function int128leToHex(...args) {
		let ra = "";
		let t = 0;
		let ta = 0;

		for (let i = 3; i >= 0; i--) {
			ta = args[i];
			t = (ta & 0xFF);
			ta = ta >>> 8;
			t = t << 8;
			t = t | (ta & 0xFF);
			ta = ta >>> 8;
			t = t << 8;
			t = t | (ta & 0xFF);
			ta = ta >>> 8;
			t = t << 8;
			t = t | ta;
			ra = ra + toZerofilledHex(t);
		}

		return ra;
	}

	// check input data type and perform conversions if needed
	let databytes = null;

	// String
	if (typeof data == 'string') {
		// convert string to array bytes
		databytes = strToBytes(data);
	} else if (data.constructor == Array) {
		if (data.length === 0) {
			// if it's empty, just assume array of bytes
			databytes = data;
		} else if (typeof data[0] == 'string') {
			databytes = charsToBytes(data);
		} else if (typeof data[0] == 'number') {
			databytes = data;
		} else {
			fflog("input data type mismatch");

			return null;
		}
	} else {
		fflog("input data type mismatch");

		return null;
	}

	// save original length
	const orgLen = databytes.length;

	// first append the "1" + 7x "0"
	databytes.push(0x80);

	// determine required amount of padding
	let tail = databytes.length % 64;

	// no room for msg length?
	if (tail > 56) {
		// pad to next 512 bit block
		for (let i = 0; i < (64 - tail); i++) {
			databytes.push(0x0);
		}

		tail = databytes.length % 64;
	}

	for (let i = 0; i < (56 - tail); i++) {
		databytes.push(0x0);
	}

	// message length in bits mod 512 should now be 448
	// append 64 bit, little-endian original msg length (in *bits*!)
	databytes = databytes.concat(int64ToBytes(orgLen * 8));

	// initialize 4x32 bit state
	let h0 = 0x67452301;
	let h1 = 0xEFCDAB89;
	let h2 = 0x98BADCFE;
	let h3 = 0x10325476;

	// temp buffers
	let a = 0,
		b = 0,
		c = 0,
		d = 0;


	function add(n1, n2) {
		return 0x0FFFFFFFF & (n1 + n2)
	}

	// function update partial state for each run
	const updateRun = function (nf, sin32, dw32, b32) {
		const temp = d;

		d = c;
		c = b;
		// b = b + rol(a + (nf + (sin32 + dw32)), b32);
		b = add(b,
			rol(
				add(a,
					add(nf, add(sin32, dw32))
				), b32
			)
		);

		a = temp;
	};


	// Digest message
	for (let i = 0; i < databytes.length / 64; i++) {
		// initialize run
		a = h0;
		b = h1;
		c = h2;
		d = h3;

		const ptr = i * 64;

		// do 64 runs
		updateRun(fF(b, c, d), 0xd76aa478, bytesToInt32(databytes, ptr), 7);
		updateRun(fF(b, c, d), 0xe8c7b756, bytesToInt32(databytes, ptr + 4), 12);
		updateRun(fF(b, c, d), 0x242070db, bytesToInt32(databytes, ptr + 8), 17);
		updateRun(fF(b, c, d), 0xc1bdceee, bytesToInt32(databytes, ptr + 12), 22);
		updateRun(fF(b, c, d), 0xf57c0faf, bytesToInt32(databytes, ptr + 16), 7);
		updateRun(fF(b, c, d), 0x4787c62a, bytesToInt32(databytes, ptr + 20), 12);
		updateRun(fF(b, c, d), 0xa8304613, bytesToInt32(databytes, ptr + 24), 17);
		updateRun(fF(b, c, d), 0xfd469501, bytesToInt32(databytes, ptr + 28), 22);
		updateRun(fF(b, c, d), 0x698098d8, bytesToInt32(databytes, ptr + 32), 7);
		updateRun(fF(b, c, d), 0x8b44f7af, bytesToInt32(databytes, ptr + 36), 12);
		updateRun(fF(b, c, d), 0xffff5bb1, bytesToInt32(databytes, ptr + 40), 17);
		updateRun(fF(b, c, d), 0x895cd7be, bytesToInt32(databytes, ptr + 44), 22);
		updateRun(fF(b, c, d), 0x6b901122, bytesToInt32(databytes, ptr + 48), 7);
		updateRun(fF(b, c, d), 0xfd987193, bytesToInt32(databytes, ptr + 52), 12);
		updateRun(fF(b, c, d), 0xa679438e, bytesToInt32(databytes, ptr + 56), 17);
		updateRun(fF(b, c, d), 0x49b40821, bytesToInt32(databytes, ptr + 60), 22);
		updateRun(fG(b, c, d), 0xf61e2562, bytesToInt32(databytes, ptr + 4), 5);
		updateRun(fG(b, c, d), 0xc040b340, bytesToInt32(databytes, ptr + 24), 9);
		updateRun(fG(b, c, d), 0x265e5a51, bytesToInt32(databytes, ptr + 44), 14);
		updateRun(fG(b, c, d), 0xe9b6c7aa, bytesToInt32(databytes, ptr), 20);
		updateRun(fG(b, c, d), 0xd62f105d, bytesToInt32(databytes, ptr + 20), 5);
		updateRun(fG(b, c, d), 0x2441453, bytesToInt32(databytes, ptr + 40), 9);
		updateRun(fG(b, c, d), 0xd8a1e681, bytesToInt32(databytes, ptr + 60), 14);
		updateRun(fG(b, c, d), 0xe7d3fbc8, bytesToInt32(databytes, ptr + 16), 20);
		updateRun(fG(b, c, d), 0x21e1cde6, bytesToInt32(databytes, ptr + 36), 5);
		updateRun(fG(b, c, d), 0xc33707d6, bytesToInt32(databytes, ptr + 56), 9);
		updateRun(fG(b, c, d), 0xf4d50d87, bytesToInt32(databytes, ptr + 12), 14);
		updateRun(fG(b, c, d), 0x455a14ed, bytesToInt32(databytes, ptr + 32), 20);
		updateRun(fG(b, c, d), 0xa9e3e905, bytesToInt32(databytes, ptr + 52), 5);
		updateRun(fG(b, c, d), 0xfcefa3f8, bytesToInt32(databytes, ptr + 8), 9);
		updateRun(fG(b, c, d), 0x676f02d9, bytesToInt32(databytes, ptr + 28), 14);
		updateRun(fG(b, c, d), 0x8d2a4c8a, bytesToInt32(databytes, ptr + 48), 20);
		updateRun(fH(b, c, d), 0xfffa3942, bytesToInt32(databytes, ptr + 20), 4);
		updateRun(fH(b, c, d), 0x8771f681, bytesToInt32(databytes, ptr + 32), 11);
		updateRun(fH(b, c, d), 0x6d9d6122, bytesToInt32(databytes, ptr + 44), 16);
		updateRun(fH(b, c, d), 0xfde5380c, bytesToInt32(databytes, ptr + 56), 23);
		updateRun(fH(b, c, d), 0xa4beea44, bytesToInt32(databytes, ptr + 4), 4);
		updateRun(fH(b, c, d), 0x4bdecfa9, bytesToInt32(databytes, ptr + 16), 11);
		updateRun(fH(b, c, d), 0xf6bb4b60, bytesToInt32(databytes, ptr + 28), 16);
		updateRun(fH(b, c, d), 0xbebfbc70, bytesToInt32(databytes, ptr + 40), 23);
		updateRun(fH(b, c, d), 0x289b7ec6, bytesToInt32(databytes, ptr + 52), 4);
		updateRun(fH(b, c, d), 0xeaa127fa, bytesToInt32(databytes, ptr), 11);
		updateRun(fH(b, c, d), 0xd4ef3085, bytesToInt32(databytes, ptr + 12), 16);
		updateRun(fH(b, c, d), 0x4881d05, bytesToInt32(databytes, ptr + 24), 23);
		updateRun(fH(b, c, d), 0xd9d4d039, bytesToInt32(databytes, ptr + 36), 4);
		updateRun(fH(b, c, d), 0xe6db99e5, bytesToInt32(databytes, ptr + 48), 11);
		updateRun(fH(b, c, d), 0x1fa27cf8, bytesToInt32(databytes, ptr + 60), 16);
		updateRun(fH(b, c, d), 0xc4ac5665, bytesToInt32(databytes, ptr + 8), 23);
		updateRun(fI(b, c, d), 0xf4292244, bytesToInt32(databytes, ptr), 6);
		updateRun(fI(b, c, d), 0x432aff97, bytesToInt32(databytes, ptr + 28), 10);
		updateRun(fI(b, c, d), 0xab9423a7, bytesToInt32(databytes, ptr + 56), 15);
		updateRun(fI(b, c, d), 0xfc93a039, bytesToInt32(databytes, ptr + 20), 21);
		updateRun(fI(b, c, d), 0x655b59c3, bytesToInt32(databytes, ptr + 48), 6);
		updateRun(fI(b, c, d), 0x8f0ccc92, bytesToInt32(databytes, ptr + 12), 10);
		updateRun(fI(b, c, d), 0xffeff47d, bytesToInt32(databytes, ptr + 40), 15);
		updateRun(fI(b, c, d), 0x85845dd1, bytesToInt32(databytes, ptr + 4), 21);
		updateRun(fI(b, c, d), 0x6fa87e4f, bytesToInt32(databytes, ptr + 32), 6);
		updateRun(fI(b, c, d), 0xfe2ce6e0, bytesToInt32(databytes, ptr + 60), 10);
		updateRun(fI(b, c, d), 0xa3014314, bytesToInt32(databytes, ptr + 24), 15);
		updateRun(fI(b, c, d), 0x4e0811a1, bytesToInt32(databytes, ptr + 52), 21);
		updateRun(fI(b, c, d), 0xf7537e82, bytesToInt32(databytes, ptr + 16), 6);
		updateRun(fI(b, c, d), 0xbd3af235, bytesToInt32(databytes, ptr + 44), 10);
		updateRun(fI(b, c, d), 0x2ad7d2bb, bytesToInt32(databytes, ptr + 8), 15);
		updateRun(fI(b, c, d), 0xeb86d391, bytesToInt32(databytes, ptr + 36), 21);

		// update buffers
		h0 = add(h0, a);
		h1 = add(h1, b);
		h2 = add(h2, c);
		h3 = add(h3, d);
	}

	// Done! Convert buffers to 128 bit (LE)
	return int128leToHex(h3, h2, h1, h0);// .toUpperCase();
}